package com.zzw.springboot.controller;

import com.zzw.springboot.util.RedisUtil;
import com.zzw.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis测试控制器
 */
@RestController
@RequestMapping("/redis")
public class RedisTestController {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 测试字符串操作
     */
    @GetMapping("/string")
    public Result testString() {
        try {
            Map<String, Object> result = new HashMap<>();

            // 存储字符串
            redisUtil.set("name", "张三");
            redisUtil.set("age", 25, 60); // 60秒过期

            // 获取字符串
            String name = (String) redisUtil.get("name");
            Integer age = (Integer) redisUtil.get("age");

            result.put("name", name);
            result.put("age", age);
            result.put("exists", redisUtil.hasKey("name"));

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("字符串操作失败: " + e.getMessage());
        }
    }

    /**
     * 测试哈希操作
     */
    @GetMapping("/hash")
    public Result testHash() {
        try {
            Map<String, Object> result = new HashMap<>();

            // 存储Hash
            redisUtil.hset("user:1", "name", "李四");
            redisUtil.hset("user:1", "age", 30);

            // 获取Hash
            String name = (String) redisUtil.hget("user:1", "name");
            Integer age = (Integer) redisUtil.hget("user:1", "age");

            result.put("name", name);
            result.put("age", age);
            result.put("allFields", redisUtil.hmget("user:1"));

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("哈希操作失败: " + e.getMessage());
        }
    }

    /**
     * 测试列表操作
     */
    @GetMapping("/list")
    public Result testList() {
        try {
            Map<String, Object> result = new HashMap<>();

            // 清除之前可能存在的列表
            if (redisUtil.hasKey("userList")) {
                redisUtil.del("userList");
            }

            // 添加列表元素
            redisUtil.lSet("userList", "张三");
            redisUtil.lSet("userList", "李四");
            redisUtil.lSet("userList", "王五");

            // 获取列表
            result.put("allUsers", redisUtil.lGet("userList", 0, -1));
            result.put("count", redisUtil.lGetListSize("userList"));

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("列表操作失败: " + e.getMessage());
        }
    }

    /**
     * 测试递增/递减
     */
    @GetMapping("/incr")
    public Result testIncr() {
        try {
            Map<String, Object> result = new HashMap<>();

            // 设置初始值
            redisUtil.set("counter", 0);

            // 递增
            Long value1 = redisUtil.incr("counter", 1);
            Long value2 = redisUtil.incr("counter", 5);

            // 递减
            Long value3 = redisUtil.decr("counter", 2);

            result.put("value1", value1);
            result.put("value2", value2);
            result.put("value3", value3);
            result.put("current", redisUtil.get("counter"));

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("递增/递减操作失败: " + e.getMessage());
        }
    }

    /**
     * 删除缓存
     */
    @DeleteMapping("/{key}")
    public Result delete(@PathVariable("key") String key) {
        try {
            Map<String, Object> result = new HashMap<>();
            result.put("exists_before", redisUtil.hasKey(key));
            redisUtil.del(key);
            result.put("exists_after", redisUtil.hasKey(key));
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除缓存失败: " + e.getMessage());
        }
    }

    /**
     * 测试集合操作
     */
    @GetMapping("/set")
    public Result testSet() {
        try {
            Map<String, Object> result = new HashMap<>();

            // 清除之前可能存在的集合
            if (redisUtil.hasKey("userSet")) {
                redisUtil.del("userSet");
            }

            // 添加集合元素
            redisUtil.sSet("userSet", "张三", "李四", "王五", "李四");

            // 获取集合
            result.put("allMembers", redisUtil.sGet("userSet"));
            result.put("count", redisUtil.sGetSetSize("userSet"));
            result.put("hasMember", redisUtil.sGet("userSet").contains("张三"));

            // 移除成员
            redisUtil.setRemove("userSet", "王五");
            result.put("afterRemove", redisUtil.sGet("userSet"));

            return Result.success(result, "集合操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("集合操作失败: " + e.getMessage());
        }
    }

    /**
     * 测试过期时间操作
     */
    @GetMapping("/expire")
    public Result testExpire(@RequestParam(defaultValue = "30") long seconds) {
        try {
            Map<String, Object> result = new HashMap<>();

            // 存储数据并设置过期时间
            String testKey = "expireTest";
            redisUtil.set(testKey, "测试过期时间", seconds);

            // 获取过期时间
            long ttl = redisUtil.getExpire(testKey);

            result.put("value", redisUtil.get(testKey));
            result.put("ttl", ttl);

            return Result.success(result, "设置过期时间成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("设置过期时间失败: " + e.getMessage());
        }
    }

    /**
     * 批量操作测试
     */
    @GetMapping("/batch")
    public Result testBatch() {
        try {
            Map<String, Object> result = new HashMap<>();

            // 批量添加数据
            for (int i = 1; i <= 5; i++) {
                redisUtil.set("batch:" + i, "批量测试-" + i);
            }

            // 批量获取数据
            Map<String, Object> batchResult = new HashMap<>();
            for (int i = 1; i <= 5; i++) {
                batchResult.put("key" + i, redisUtil.get("batch:" + i));
            }

            result.put("batchData", batchResult);

            return Result.success(result, "批量操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("批量操作失败: " + e.getMessage());
        }
    }
}
